<?php
/**
 * 会员来访记录
 * @return
 * @author yuncran@126.com
 */

namespace app\model;

use app\BaseModel;
use app\CacheKey;
use app\FieldKey;

class UserComeVisitModel extends BaseModel
{

    protected $pk = "id";

    protected $name = "user_come_visit";

//    protected $append = ['user'];

    public function getUser()
    {
        return $this->hasOne(UserModel::class, 'id', 'user_id')
            ->field(FieldKey::user_field_has_one)
            ->append(['level_result'])->withAttr('level_result', function ($value, $data) {
                return UserLevelModel::where(['id' => $data['level']])->cache(CacheKey::level_title)->find();
            });
    }


    public static function saveIn(int $user_id, int $to_user_id): int
    {
        return self::getModel()->save([
            'user_id' => $user_id,
            'to_user_id' => $to_user_id,
            'create_time' => date('Y-m-d H:i:s', time()),
            'update_time' => date('Y-m-d H:i:s', time()),
        ]);
    }


}